<?php

namespace App\Http\Controllers\Account;


use App\Rules\VerificationCodeRule;
use App\Services\CodeService;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;

class RegisterController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct()
    {
        // $this->middleware('guest')->except('code');
    }

    public function index()
    {
        //
        return view('account.register');
    }

    public function store(Request $request, User $user)
    {

        $request->validate(
            [

                'account' => ['required', $this->validateAccountField(), Rule::unique('users', $this->username())],
                'password' => ['required', 'min:3', 'confirmed'],
                'name' => ['required'],
                'code' => ['required', new VerificationCodeRule($request->account)],
            ],
            ['code.required' => '验证码不能为空']
        );

        $user->password = $request->password;
        $user->name = $request->name;
        $user[$this->username()] = $request->account;
        $user->save();
        Auth::login($user);
        return response()->json(['message' => '注册成功']);
    }

}
